"vim自带的适配Windows环境的配置文件
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      if empty(&shellxquote)
        let l:shxq_sav = ''
        set shellxquote&
      endif
      let cmd = '"' . $VIMRUNTIME . '\diff"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  if exists('l:shxq_sav')
    let &shellxquote=l:shxq_sav
  endif
endfunction

"显示函数
set nu!
"设置主题颜色
colorscheme torte
"打开语法显示
syntax enable
syntax on
"文件改动时自动载入
set autoread
"高亮显示结果
set hlsearch
"输入搜索文字时，vim实时匹配
set incsearch
"允许使用空格键
set backspace=indent,eol,start whichwrap+=<,>,[,]
"设置允许自动换行
set   wrap
set   whichwrap=b,s,<,>,[,]
"设置整词换行
set   linebreak
"设置历史
set    history=1000
"设置匹配模式，输入左括号会匹配右括号
set   showmatch
"不高亮当前行
set   nocursorline
"设置状态行
highlight StatusLine guifg=SlateBlue guibg=yellow
highlight StatusLineNC guifg=Gray guibg=White
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"我的状态行显示的内容（包括文件类型和解码）
"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
"在编辑过程中，在右下角显示光标位置的状态行
set ruler
"命令行（在状态行下）的高度，默认为1，这里是2
set cmdheight=1
"搜索时忽视大小写
set    ignorecase
"打开文件类型自动检测功能
filetype   on
"将tab设置为拓展的tab（变成空格）
set expandtab
"制表符为4个空格宽度
set tabstop=4
"使得空格也可以删除掉4个空格的制表符
set softtabstop=4
set shiftwidth=4
"自动格式化
set formatoptions=tcrqn
"为c语言提供自动缩进
set smartindent
"继承前一行的缩进模式
set autoindent
"将tab的缩进改为4个空格
%ret! 4
"总是显示状态行
set laststatus=2
"设置leader按键为逗号
let mapleader=","
"不让vim生成讨厌的~后缀的文件
set nobackup
"让vim关闭后还可以撤销操作（似乎不能用）
set undofile
set undodir='$VIM/vimfiles/undodir'
set undolevels=100 "maximum number of changes that can be undone
"设置鼠标一直可用
set   mouse=a
set   selection=exclusive
set   selectmode=mouse,key

"解决某些文件打开编码出错（乱码）的问题（这个顺序是比较好的）
"同时还要求所有vim相关的目录不能有中文名，其中最容易出问题的就是TEMP环境变量的目录，注意！
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
language messages zh_CN.utf-8

"ctags的设置
set tags=tags;
set autochdir

"set guifont=Courier_new:h10:b:cDEFAULT
"set guifont=SimSun-ExtB:h11:b
"set guifont=Ubuntu_Mono:h12:cANSI
"set guifont=Inconsolata:h12:cDEFAULT
"set guifont=Monaco:h11:cANSI
set guifont=DejaVu_Sans_Mono_for_Powerline:h10:cANSI
"set guifont=DejaVu_Sans_Mono:h10:cANSI

"高亮当前行
set cursorline
"编辑时上下至少保持的行数
set scrolloff=3
"使能折叠代码
set foldenable
"使新切割出来的窗口在右边
set splitright

"高亮有问题的空格，以及tab键（总之是一系列的特殊符号）
set list
set listchars=tab:›\ ,trail:•,extends:#,nbsp:. " Highlight problematic whitespace

"从spf13上面拷贝过来的语句，不知道有什么作用。。。
"set wildmenu                    " Show list instead of just completing
"set wildmode=list:longest,full  " Command <Tab> completion, list matches, then longest common part, then all.
"set whichwrap=b,s,h,l,<,>,[,]   " Backspace and cursor keys wrap too

"启动的时候隐藏菜单栏和工具栏，可以看起来更舒服些
if has("gui_running")
"au GUIEnter * simalt ~x " 窗口启动时自动最大化
"set guioptions-=m " 隐藏菜单栏
set guioptions-=T " 隐藏工具栏
"set guioptions-=L " 隐藏左侧滚动条
"set guioptions-=r " 隐藏右侧滚动条
"set guioptions-=b " 隐藏底部滚动条
"set showtabline=0 " 隐藏Tab栏
"设定默认窗口大小
set lines=38 columns=118
endif

"设定输入vim命令的时候自动补全，同同时还会展示可能匹配的命令
"就有点类似于bash的tab
set wildmode=longest:list,full
set wildmenu
set whichwrap=b,s,h,l,<,>,[,]   " Backspace and cursor keys wrap too

"修改swp文件的位置
set directory=$VIM/vimfiles/swapfiles//

"----------------一下为常用快捷键配置---------------"
"快速运行python文件
"map <C-D> :!python3 %<cr>
map <C-E> :!python %<cr>

"代码折叠等级快捷键设置
" Code folding options
nmap <leader>f0 :set foldlevel=0<CR>
nmap <leader>f1 :set foldlevel=1<CR>
nmap <leader>f2 :set foldlevel=2<CR>
nmap <leader>f3 :set foldlevel=3<CR>
nmap <leader>f4 :set foldlevel=4<CR>
nmap <leader>f5 :set foldlevel=5<CR>
nmap <leader>f6 :set foldlevel=6<CR>
nmap <leader>f7 :set foldlevel=7<CR>
nmap <leader>f8 :set foldlevel=8<CR>
nmap <leader>f9 :set foldlevel=9<CR>

"快速切换Buffer的快捷键
noremap <C-Down> <C-W>j
noremap <C-Up> <C-W>k
noremap <C-Left> <C-W>h
noremap <C-Right> <C-W>l
"noremap <C-TAB> :bn<cr>
"noremap <C-S-TAB> :bp<cr>

"快速添加git修改（git add）
nmap <leader>ga :!git add %<cr>

"快速打开文件
nmap <leader>o :browse confirm e<cr>

"快速设置文件类型为python(为scons定制)
nmap <leader>sc :set filetype=python<cr>

"快速删除行尾多余的空格
func! DeleteTrailingWS()
  exe "normal mz"
  %s/\s\+$//ge
  exe "normal `z"
endfunc
"将文件进行标准格式化：
"   1.将tab全部转换成空格
"   2.强多余的行尾空格去掉
func! FormatFiles()
    exe "retab"
    call DeleteTrailingWS()
endfunc
noremap <leader>ds :call DeleteTrailingWS()<CR>
noremap <leader>fm :call FormatFiles()<CR>

"---------------以下为插件相关配置------------------"
" 引用自定义的vundle配置文件
source $VIM/vundle_vimrc "存放vimrc的地方(不是固定写法，可自定义)

" ==============================================
" 插件：vimcdoc
" 功能：vim的中文文档
Plugin 'vimcn/vimcdoc'

" ==============================================
" 插件：vim-fugitive
" 功能：神一般的vim+git插件
Plugin 'tpope/vim-fugitive'
nnoremap <silent> <leader>gs :Gstatus<CR>
nnoremap <silent> <leader>gd :Gdiff<CR>
nnoremap <silent> <leader>gc :Gcommit<CR>
nnoremap <silent> <leader>gb :Gblame<CR>
nnoremap <silent> <leader>gl :Glog<CR>
nnoremap <silent> <leader>gp :Git push<CR>
nnoremap <silent> <leader>gr :Gread<CR>
nnoremap <silent> <leader>gw :Gwrite<CR>
nnoremap <silent> <leader>ge :Gedit<CR>
nnoremap <silent> <leader>gi :Git add -p<CR>
nnoremap <silent> <leader>gt :SignifyToggle<CR>

"===============================================
"插件：grep.vim
"用来搜索文件用的，类似于vim下面的grep命令
Plugin 'grep.vim'
nnoremap <silent> <F3> :Grep<CR>

"===============================================
"minibufexpl.vim使用配置
"Plugin 'minibufexpl.vim'
"let g:miniBufExplMapCTabSwitchBufs=1
"let g:miniBufExplMapWindowsNavVim=1
"let g:miniBufExplMapWindowNavArrows=1

"===============================================
"插件：minibufexpl.vim
"说明：github上的minibufexpl的改进版本
"      注：经测试还是有BUG，会知道窗口自动调整大小，很不爽！
"Plugin 'fholgado/minibufexpl.vim'

"===============================================
"插件：bufexplorer
"说明：浏览buf的插件
Plugin 'jlanzarotta/bufexplorer'

"===============================================
"Taglist使用的设置
"Plugin 'taglist.vim'
"let Tlist_Show_One_File=1
"let Tlist_Exit_OnlyWindow=1

"===============================================
"插件：SuperTab
"功能：按tab实现自动补全
"VimScript上维护的这个版本太老了，不好用，所以我另外安装了github上的最新版本
"Plugin 'SuperTab'
"Plugin 'https://github.com/ervandew/supertab.git'
"let g:SuperTabRetainCompletionType=2

"===============================================
"插件：Mark
"功能：高亮某些关键字
Plugin 'vim-scripts/Mark--Karkat'
let g:mwDefaultHighlightingPalette = 'extended'

"===============================================
"插件：a.vim
"功能：在C文件和H文件间快速切换
Plugin 'a.vim'

"===============================================
"插件：Visual Mark
"功能：为代码插入书签，方便快速跳转
Plugin 'Visual-Mark'

"===============================================
"YouCompleteMe 相关设置
"因为YouCompleteMe好像不太好用（Windows）
"所以准备丢弃YCM的设置
source $VIM/opt_vimrc/ycm.vimrc

"===============================================
"neocomplete 相关设置
"source $VIM/opt_vimrc/neocomplete.vimrc

source $VIM/opt_vimrc/ultisnip.vimrc

"source $VIM/opt_vimrc/neosnip.vimrc

"===============================================
"插件：Pydiction
"功能：为python提供自动补全功能
"Plugin 'Pydiction'
let g:pydiction_location = '$VIM\vimfiles\bundle\Pydiction\complete-dict'
let g:pydiction_menu_height = 20 "设置弹出菜单的高度，默认是15

"===============================================
"插件：jedi-vim(python)
"功能：强大的python自动补全插件
"注意：需要先在python当中安装jedi库
"注意：似乎不能在python3下工作
"Plugin 'davidhalter/jedi-vim'
"取消分割窗口提示补全函数的功能
autocmd FileType python setlocal completeopt-=preview
"将neocomplete的补全方法改为使用jedi的补全
autocmd FileType python setlocal omnifunc=jedi#completions
"在检测到文件类型为python的时候自动将
"<Ctrl-G>设置为跳转到定义的快捷键
function SetJediGotoShort()
    noremap <C-G> :call jedi#goto()<CR>
endfunction
"autocmd FileType python call SetJediGotoShort()
"下面是网上一个推荐的设置，不过好像没有什么很大的用处
"连接：https://plumz.me/archives/839/
"let g:neocomplete#enable_auto_select = 0
"let g:jedi#popup_select_first=0
"set completeopt=longest,menuone
"let g:jedi#auto_vim_configuration = 0
"let g:jedi#popup_on_dot = 0
"if !exists('g:neocomplete#force_omni_input_patterns')
"        let g:neocomplete#force_omni_input_patterns = {}
"endif
"let g:neocomplete#force_omni_input_patterns.python = '\%([^. \t]\.\|^\s*@\)\w*'

"===============================================
"插件：syntastic
"功能：为多种语言提供语法检查
Plugin 'scrooloose/syntastic'
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
"设置快捷键以关闭syntastic的语法检查
"（可以解决有时候会让打开文件变得特别慢的问题）
nmap <leader>s :SyntasticToggleMode<cr>
"禁用部分文件的语法检查
let g:syntastic_mode_map = {
    \ "mode": "active",
    \ "active_filetypes": [],
    \ "passive_filetypes": ["tex"] }
"设置python的语法检查基于python
let g:syntastic_python_python_exec = 'python'

"===============================================
"插件：vim-airline
"功能：增强状态栏显示状态
Plugin 'vim-airline/vim-airline'
let g:airline_powerline_fonts = 1
"使能tabline（显示buffer）
let g:airline#extensions#tabline#enabled = 1
"let g:airline#extensions#tabline#left_sep = ' '
"let g:airline#extensions#tabline#left_alt_sep = '|'
"使能tabline显示buffer的编号
let g:airline#extensions#tabline#buffer_nr_show = 1
"使能syntastic（语法检查）
let g:airline#extensions#syntastic#enabled = 1


"===============================================
"插件：vim-airline-themes
"功能：提供vim-airline的不同主题，具体的样子可以参见如下链接：
"      https://github.com/vim-airline/vim-airline/wiki/Screenshots
Plugin 'vim-airline/vim-airline-themes'

"===============================================
"插件：ctrlp.vim
"功能：搜索文件
Plugin 'ctrlpvim/ctrlp.vim'
nmap <leader>p :CtrlP<cr>
nmap <leader>pb :CtrlPBuffer<cr>

"===============================================
"插件：undotree
"功能：显示撤销树(同时永久保存撤销历史)
Plugin 'mbbill/undotree'
nmap <silent><leader>u :UndotreeToggle<cr>
if has("persistent_undo")
    set undodir=$VIM/vimfiles/undodir/
    set undofile
endif

"===============================================
"插件：nerdtree
"功能：提供目录树视图
Plugin 'scrooloose/nerdtree'
nmap <silent><leader>e :NERDTreeFind<CR>
let NERDTreeQuitOnOpen=1
"改变文件后缀高亮(但是不知道为什么不支持c的后缀)
function! NERDTreeHighlightFile(extension, fg, bg, guifg, guibg)
 exec 'autocmd FileType nerdtree highlight ' . a:extension .' ctermbg='. a:bg .' ctermfg='. a:fg .' guibg='. a:guibg .' guifg='. a:guifg
 exec 'autocmd FileType nerdtree syn match ' . a:extension .' #^\s\+.*'. a:extension .'$#'
endfunction
call NERDTreeHighlightFile('jade', 'green', 'none', 'green', '#151515')
call NERDTreeHighlightFile('ini', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('md', 'blue', 'none', '#3366FF', '#151515')
call NERDTreeHighlightFile('yml', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('config', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('conf', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('json', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('html', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('styl', 'cyan', 'none', 'cyan', '#151515')
call NERDTreeHighlightFile('css', 'cyan', 'none', 'cyan', '#151515')
call NERDTreeHighlightFile('coffee', 'Red', 'none', 'red', '#151515')
call NERDTreeHighlightFile('js', 'Red', 'none', '#ffa500', '#151515')
call NERDTreeHighlightFile('php', 'Magenta', 'none', '#ff00ff', '#151515')
call NERDTreeHighlightFile('cpp', 'green', 'none', 'green', '#151515')
call NERDTreeHighlightFile('h', 'green', 'none', 'green', '#151515')
"call NERDTreeHighlightFile('c', 'green', 'none', 'green', '#151515')
"
"===============================================
"插件：tagbar
"功能：比taglist好用的tag显示
Plugin 'majutsushi/tagbar'
nmap <silent><leader>tt :TagbarToggle<CR>

"===============================================
"插件：WinManager
"功能：用来显示常用的两个窗口
"Plugin 'WinManager'
let g:winManagerWindowLayout='FileExplorer|TagList'
let g:persistentBehaviour = 0
let g:winManagerWidth = 20
let g:defaultExplorer = 1
nmap <silent><leader>wm :WMToggle<cr>

"===============================================
"插件：nerdcommenter
"功能：快速注释插件
Plugin 'scrooloose/nerdcommenter'

" ==============================================
"插件：delimitMate
"功能：自动补全单引号，双引号等
Plugin 'Raimondi/delimitMate'
" for python docstring ", 特别有用
au FileType python let b:delimitMate_nesting_quotes = ['"']
" 关闭某些类型文件的自动补全
"au FileType mail let b:delimitMate_autoclose = 0
"设置自动补全括号（不好用）
"let g:delimitMate_expand_cr = 1
"设置自动补全空格
let g:delimitMate_expand_space = 1
"因为Shift+Tab被占用了，所以现在绑定Ctrl+F来实现跳转到括号后的功能。
imap <C-f> <Plug>delimitMateS-Tab

" ==============================================
" 插件：auto-pair
" 功能：自动补全括号等一对的符号
"Plugin 'auto-pairs'

"===============================================
"插件：molokai
"功能：最经典的vim配色
Plugin 'tomasr/molokai'
colorscheme molokai

"===============================================
"插件：vim-gitgutter
"功能：在有Git的目录下，显示当前编辑文件的修改情况
Plugin 'airblade/vim-gitgutter'
let g:gitgutter_enabled = 0
"让gitgutter只在写入文件的时候才更新，这样就不会变得很卡
let g:gitgutter_realtime = 0
let g:gitgutter_eager = 0
nmap <leader>gg :GitGutterToggle<cr>

"===============================================
"插件：gitv
"功能：在Vim中显示git的历史信息(需要依赖fugitive)
Plugin 'gregsexton/gitv'
nmap <leader>gv :Gitv --all<cr>
nmap <leader>gV :Gitv! --all<cr>
vmap <leader>gV :Gitv! --all<cr>
"防止在更新信息的时候频繁刷新屏幕，导致体验不好
set lazyredraw

"===============================================
"插件：vimagit
"功能：相当于git的一个图形化客户端
source $VIM/opt_vimrc/vimagit.vimrc

"===============================================
"插件：hypergit
"功能：未知，因为暂时还没能运行起来...
"Plugin 'c9s/hypergit.vim'
"nmap <leader>G :ToggleGitMenu<CR>

"===============================================
"插件：bim-bufferline
"功能：在vim的命令栏里面显示buffer的编号
"      然后输入命令（vim自带功能）:b1就可以切换到1号的buffer了
"      注：如果有了vim-airline的buffer功能，这个就可以不要了
"      注：vim-airline和这个插件是兼容的！！！
"Plugin 'bling/vim-bufferline'
"快速切换到1~9号buffer
nmap <A-1> :b1<cr>
nmap <A-2> :b2<cr>
nmap <A-3> :b3<cr>
nmap <A-4> :b4<cr>
nmap <A-5> :b5<cr>
nmap <A-6> :b6<cr>
nmap <A-7> :b7<cr>
nmap <A-8> :b8<cr>
nmap <A-9> :b9<cr>

"===============================================
"插件：C-vim
"功能：用来快速插入C/C++模板
Plugin 'c.vim'

"===============================================
"插件：vimtex
"功能：为vim增加LaTex支持
"注：
"   快捷键：
"       \lt 打开文档章节索引
"       \ly 打开文档标志（label）索引
Plugin 'lervag/vimtex'
"禁用PDF阅读器（不然会报错）
let g:vimtex_view_enabled = 0
"使能YouCompleteme在tex文件中的补全
if !exists('g:ycm_semantic_triggers')
    let g:ycm_semantic_triggers = {}
endif
let g:ycm_semantic_triggers.tex = [
    \ 're!\\[A-Za-z]*cite[A-Za-z]*(\[[^]]*\]){0,2}{[^}]*',
    \ 're!\\[A-Za-z]*ref({[^}]*|range{([^,{}]*(}{)?))',
    \ 're!\\includegraphics\*?(\[[^]]*\]){0,2}{[^}]*',
    \ 're!\\(include(only)?|input){[^}]*'
    \ ]

"==============================================
"插件：vim-flake8
"功能：Python用来检查代码规范的工具。
"      需要安装Python的flake8工具。
"      注：该插件与Syntastic插件兼容！
"Plugin 'nvie/vim-flake8'

"==============================================
"插件：vim-python-pep8-indent
"功能：按照pep8规范进行python文件的缩进
"Plugin 'hynek/vim-python-pep8-indent'

"==============================================
"插件：vim-startify
"功能：更改vim开始界面
Plugin 'mhinz/vim-startify'
set viminfo='100,n$VIM/_viminfo

"==============================================
"插件：vim-indent-guides
"功能：优化缩进显示提醒
Plugin 'nathanaelkane/vim-indent-guides'
let g:indent_guides_start_level = 2
let g:indent_guides_guide_size = 1
let g:indent_guides_enable_on_vim_startup = 1

"==============================================
"插件：conflict-marker
"功能：用于快速定位版本控制系统带来的冲突
Plugin 'rhysd/conflict-marker.vim'

"==============================================
"插件：DoxygenToolKit
"功能：用于自动生成Doxygen注释
Plugin 'mrtazz/DoxygenToolkit.vim'
let g:DoxygenToolkit_briefTag_pre="@brief  "
let g:DoxygenToolkit_paramTag_pre="@param  "
let g:DoxygenToolkit_returnTag="@retval "
"let g:DoxygenToolkit_blockHeader="-------------------------------"
"let g:DoxygenToolkit_blockFooter="---------------------------------"
let g:DoxygenToolkit_authorName="Yang Xiang"
let g:DoxygenToolkit_licenseTag="Copyright (c) 2016 Intel. All rights reserved."
nmap <leader>df :Dox<CR>
nmap <leader>da :DoxAuthor<CR>
nmap <leader>dl :DoxLic<CR>

"=============================================
"插件：VimIM
"功能：Vim的输入法
"注：好像利用了omni complete的功能，在GVim下用起来很慢
"Plugin 'vim-scripts/VimIM'

let g:vimim_cloud = 'sogou,baidu,qq,google'
let g:vimim_map = 'tab_as_gi'
"let g:vimim_mode = 'dynamic'
"let g:vimim_mycloud = 0
"let g:vimim_plugin = 'C:/var/mobile/vim/vimfiles/plugin'
"let g:vimim_punctuation = 2
"let g:vimim_shuangpin = 0
"let g:vimim_toggle = 'pinyin,google,sogou'

"=============================================
"插件：vim-markdown
"功能：未知
Plugin 'plasticboy/vim-markdown'

"=============================================
"插件：FastFold
"功能：解决vim-markdown和neocomplete的冲突问题
"      冲突主要体现在折叠上
Plugin 'Konfekt/FastFold'

"=============================================
"插件：Rainbow
"功能：用于用不同的颜色高亮括号对
Plugin 'oblitum/rainbow'
"仅仅在以下这些后缀的文件名中启用
au FileType c,cpp,h,hpp,python call rainbow#load()

"=============================================
"插件：python-mode
"功能：用于python的语法检查，运行等功能
"注意：<leader>r被绑定为运行python脚本的功能
Plugin 'python-mode/python-mode'
let g:pymode_run = 1
let g:pymode_run_bind = '<leader>r'
